package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 查询菜单
     *
     * @param username
     * @return
     */
//    @PreAuthorize("hasAuthority('MENU_QUERY')")
    @RequestMapping("/findAll")
    public Result findAll(String username) {
        try {
            String menu = jedisPool.getResource().get(username);
            if (menu == null){
                List<Menu> menuList = menuService.findAll(username);
                return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
            }else {
                List<Menu> menuList = JSONArray.parseArray(menu, Menu.class);
                return new Result(true, MessageConstant.GET_MENU_SUCCESS, menuList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }

    /**
     * 查询所有的菜单信息
     *
     * @return
     */
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        try {
            List<Menu> menuList = menuService.findAllMenu();
            return new Result(true, MessageConstant.GET_MENU_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_FAIL);
        }
    }


}
